<template>
    <div class="main">

        <div style="width: 100%;height: 0.8vh;"></div>
        <div style="position: relative;height: 5vh;width: 97%;
        margin-left: 1.8%;box-shadow: 0px 0px 1px 1PX rgb(242, 240, 240);
        display: flex;align-items: center;">
            <el-button icon="el-icon-back" plain type="primary"
                @click="returnRoles()">返回</el-button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            队伍名字 &nbsp;<el-input placeholder="请输入内容" prefix-icon="el-icon-search" v-model="serchMapUserRole.army_nameLike"
                clearable style="width: 18vw;">&nbsp;</el-input>&nbsp;
            <el-button icon="el-icon-search" plain type="primary" @click="SerchMore">查询</el-button>
        </div>
        <div style="width: 100%;height: 0.8vh;"></div>
        <div style="position: relative;width: 98%;
        margin-left: 1.8%;box-shadow: 0px 0px 1px 1PX rgb(242, 240, 240);
        display: flex;align-items:flex-star t;height: 80vh;display: flex;">

            <div
                style="position: relative;width: 28%;height: 100%;display: flex;justify-content: center;align-items: center;">
                <div style="position: relative;width: 94%;height: 99%;">
                    <span>用户军队职务详情</span>
                    <div style="height: 1vh;width: 100%;"></div>
                    <el-tree :data="listUserRole" node-key="duty_id" default-expand-all :props="defaultProps"
                        :expand-on-click-node="false" v-if="getUserArmyX.army_id != null">
                        <span class="custom-tree-node" slot-scope="{ node, data }">
                            <span>{{ node.label }}</span>
                            &nbsp;&nbsp;&nbsp;&nbsp;
                            <span>
                                <el-button type="text" size="mini" style="color: rgb(255, 0, 0);" icon="el-icon-delete"
                                    @click="() => remove(node, data)">
                                    删除
                                </el-button>
                            </span>
                        </span>
                    </el-tree>
                </div>
            </div>

            <div style="position: relative;width: 72%;height: 70vh;">
                <div style="position: relative;width: 100%;height: 70vh;position: relative;overflow-y: scroll;">
                    <el-table :data="list" tooltip-effect="dark" row-key="army_id">
                        <el-table-column prop="army_name" label="军队名字" width="190" :show-overflow-tooltip="true">
                        </el-table-column>
                        <el-table-column label="查看职务" type="expand" width="400">
                            <template slot-scope="props">
                                <el-table :data="props.row.dutyUserzList" v-if="props.row.dutyUserzList.length > 0">
                                    <el-table-column width="30">
                                    </el-table-column>
                                    <el-table-column prop="duty_name" label="职务名称" width="120">
                                    </el-table-column>
                                    <el-table-column label="职务状态" width="100">
                                        <template v-slot="scope">
                                            <el-tag v-if="scope.row.user_id == null" type="success">空闲</el-tag>
                                            <el-tag v-else type="danger">已被授予</el-tag>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="操作" width="300">
                                        <template v-slot="scope">
                                            <el-button type="text" @click="() => toUserDuty(scope.row)" icon="el-icon-plus"
                                                v-if="scope.row.user_id == null">
                                                授予
                                            </el-button>
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" width="300">
                            <template v-slot="scope">
                                <el-button type="text" @click="() => toUserArmy(scope.row)" icon="el-icon-edit"
                                    v-if="scope.row.army_id != getUserArmyX.army_id">
                                    更改军队
                                </el-button>
                                <el-tag v-else type="success">我的军队</el-tag>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <!--分页-->
                <el-pagination @size-change="sizeData" @current-change="pageData" :page-sizes="[5, 10]" :page-size="5"
                    layout="total, sizes, prev, pager, next, jumper" :total="count">
                </el-pagination>
            </div>

        </div>




    </div>
</template>
  
<script>

import DutyzxApi from '@/api/dutyzxRequest.js'
import ArmyXZApi from '@/api/ArmyxRequest.js'
import UserArmyApi from '@/api/userzarmyxuRequest.js'
import UserDutyApi from '@/api/userzdutyzxRequest.js'
export default {
    // 组件名称
    name: 'userarmyduty',
    // 组件参数 接收来自父组件的数据
    props: {},
    // 局部注册的组件
    components: {},
    // 组件状态值
    data() {
        return {
            //存放用户军队职位
            listUserRole: [
                {
                    duty_id: 0,
                    duty_name: '所属军队',
                    children: [],
                }
            ],
            //存放军队和职位信息
            list: [],
            //用户角色树展示
            defaultProps: {
                children: 'children',
                label: 'duty_name'
            },
            //查询用户军队和军队职务
            serchMapUserRole: {
                army_id: null,
                army_nameLike: null,
                pageNum: 1,
                pageSize: 5,
            },
            //军队有多少个
            count: 10,
            //用户所属军队ID
            army_id_p: 0,
        }
    },
    // 计算属性
    computed: {
        //获取用户ID
        getVuexsUserID() {
            return this.$store.getters['manageuser/getdisplayUserID'];
        },
        //获取用户军队信息
        getUserArmyX() {
            return this.$store.getters['manageuser/getdisplayArmy'];
        }
    },
    // 侦听器
    watch: {

    },
    // 组件方法
    methods: {
        //返回用户信息页面
        returnRoles() {
            this.$store.commit("manageuser/insertdisplayUserDemo", 'userinfor');
        },
        //查询数量
        fetchData() {
            this.listUserRole[0].duty_name = this.getUserArmyX.army_name;
            this.army_id_p = this.getUserArmyX.army_id;

            let obj = {
                user_id: this.getVuexsUserID,
            }
            DutyzxApi.selectoneUserDuty(obj).then(res => {
                this.listUserRole[0].children = res.data;
            })
            ArmyXZApi.selectArmyDutyUserUser(this.serchMapUserRole).then(res => {
                this.list = res.data.data;
                this.count = res.data.total;
                if (res.data.data.length <= 0 && this.serchMapUserRole.pageNum > 1) {
                    this.serchMapUserRole.pageNum--;
                    ArmyXZApi.selectArmyDutyUserUser(this.serchMapUserRole).then(res => {
                        this.list = res.data.data;
                        this.count = res.data.total;
                    });
                }
            })
        },//查询更多
        SerchMore() {
            if (this.serchMapUserRole.army_nameLike == '') this.serchMapUserRole.army_nameLike = null;
            this.serchMapUserRole.pageNum = 1;
            this.serchMapUserRole.pageSize = 5;
            this.fetchData();
        },
        //当分页大小产生变化的时候调用该方法
        sizeData(size) {
            this.serchMapUserRole.pageSize = size - 1
            this.fetchData()
        },
        pageData(page) {
            this.serchMapUserRole.pageNum = page
            this.fetchData()
        },
        //删除用户军队或职务
        remove(node, data) {
            this.$confirm('您确定要删除吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                if (data.duty_id === 0) {

                    if (this.listUserRole[0].children.length > 0) {
                        this.$message({
                            type: 'error',
                            message: '存在职位不可删除'
                        });
                        return;
                    } else {
                        let obj = {
                            army_id: this.getUserArmyX.army_id,
                            user_id: this.getVuexsUserID,
                        }
                        UserArmyApi.deleteUserArmy(obj).then(res => {
                            this.army_id_p = 0;
                            this.$store.commit("manageuser/insertdisplayArmy", {
                                army_id: null,
                                army_name: null,
                            });
                            this.serchMapUserRole.army_nameLike = null;
                            this.fetchData();
                        })

                    }

                } else {
                    //删除用户职位
                    let obj = {
                        duty_id: data.duty_id,
                        user_id: this.getVuexsUserID,
                    }
                    UserDutyApi.deleteUserDuty(obj).then(res => {
                        this.fetchData();
                    })

                }

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //给用户职务
        toUserDuty(data) {
            if (this.getUserArmyX.army_id === null) {
                this.$message({
                    type: 'error',
                    message: '请先给用户授予队伍'
                });
            } else if (this.getUserArmyX.army_id === data.army_id) {
                let obj = {
                    duty_id: data.duty_id,
                    user_id: this.getVuexsUserID,
                }
                UserDutyApi.insert(obj).then(res => {
                    this.fetchData();
                })
            } else {
                this.$message({
                    type: 'error',
                    message: '职位与用户军衔不符合'
                });
            }
        },
        //给用户军队
        toUserArmy(data) {
            if (this.getUserArmyX.army_id === null) {
                let obj = {
                    army_id: data.army_id,
                    user_id: this.getVuexsUserID,
                }
                UserArmyApi.insertOrUpdate(obj).then(res => {
                    this.$store.commit("manageuser/insertdisplayArmy", {
                        army_id: data.army_id,
                        army_name: data.army_name,
                    });
                    this.serchMapUserRole.army_nameLike = data.army_name;
                    this.fetchData();
                })
                this.serchMapUserRole.army_nameLike = data.army_name;
            } else if (this.listUserRole[0].children.length > 0) {
                this.$message({
                    type: 'error',
                    message: '用户在原来队伍海存在官职，不可更换'
                });
            }

        }
    },
    // 以下是生命周期钩子   注：没用到的钩子请自行删除
    /**
    * 在实例初始化之后，组件属性计算之前，如data属性等
    */
    beforeCreate() {
    },
    /**
    * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
    */
    created() {

        this.serchMapUserRole.army_nameLike = this.getUserArmyX.army_name;
        this.fetchData();
    },
    /**
    * 在挂载开始之前被调用：相关的 render 函数首次被调用。
    */
    beforeMount() {

    },
    /**
    * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
    * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
    */
    mounted() {
    },
    /**
    * 数据更新时调用，发生在虚拟 DOM 重新渲染和打补丁之前。
    * 你可以在这个钩子中进一步地更改状态，这不会触发附加的重渲染过程。
    */
    beforeUpdate() {
    },
    /**
    * 由于数据更改导致的虚拟 DOM 重新渲染和打补丁，在这之后会调用该钩子。
    * 当这个钩子被调用时，组件 DOM 已经更新，所以你现在可以执行依赖于 DOM 的操作。
    */
    updated() {
    },
    /**
    * keep-alive 组件激活时调用。 仅针对keep-alive 组件有效
    */
    activated() {
    },
    /**
    * keep-alive 组件停用时调用。 仅针对keep-alive 组件有效
    */
    deactivated() {
    },
    /**
    * 实例销毁之前调用。在这一步，实例仍然完全可用。
    */
    beforeDestroy() {
    },
    /**
    * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
    * 所有的事件监听器会被移除，所有的子实例也会被销毁。
    */
    destroyed() {
    }
}
</script> 
  
  
<style scoped ></style>
  